package com.kaiwukj.android.ufamily.mvp.ui.page.keeper;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerKeeperComponent;
import com.kaiwukj.android.ufamily.di.module.KeeperModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.KeeperEvent;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.message.ChatActivity;
import com.kaiwukj.android.ufamily.mvp.ui.widget.RefreshLayout;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.KeeperPickUrl)
public class KeeperPickActivity extends BaseMvpActivity<KeeperPresenter> implements KeeperContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_keeper_desc)
    TextView tvKeeperDesc;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private KeeperPickerAdapter mPickerAdapter;

    @Autowired
    int flag = 0;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_keeper_picker;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerKeeperComponent.builder()
                .appComponent(appComponent)
                .keeperModule(new KeeperModule(this))
                .build().inject(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (flag > 0) {
            tvKeeperDesc.setText("请联系以下官方客服");
            tvTitle.setText("客服");
        } else {
            tvKeeperDesc.setText("当前小区有以下管家");
            tvTitle.setText("我的管家");
        }
        refreshLayout.setOnRefreshListener(layout -> refreshData());
        rvList.setLayoutManager(new LinearLayoutManager(this));
        mPickerAdapter = new KeeperPickerAdapter();
        if (flag > 0) {
            mPickerAdapter.setFlag(flag);
        }
        rvList.setAdapter(mPickerAdapter);
        rvList.addItemDecoration(new SpaceItemDecoration(18));
        mPickerAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.btn_detail:
                    String phone = mPickerAdapter.getItem(position).getPhone();
                    if (!StringUtils.isEmpty(phone)) {
                        showMessageOKCancel(this, String.format("拨打: %s", phone), v -> {
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            intent.setData(Uri.parse("tel:" + phone));
                            startActivity(intent);
                        });
                    } else {
                        showMessage("获取联系方式失败!");
                    }
                    break;
                case R.id.btn_pick:
                    if (flag > 0) {
                        String hxName = mPickerAdapter.getItem(position).getHxName();
                        if (!StringUtils.isEmpty(hxName)) {
                            ChatActivity.startSingleChat(this, hxName);
                        }
                    } else {
                        mPresenter.bindKeeper(String.valueOf(adapter.getItemId(position)), true);
                    }
                    break;
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        refreshData();
        emptyView.show(true);
    }

    private void refreshData() {
        if (flag > 0) {
            mPresenter.fetchSysKeeperList();
        } else {
            mPresenter.fetchKeeperList();
        }

    }

    @Override
    public void showKeeperInfo(KeeperResult result) {

    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        refreshLayout.finishRefresh(false);
    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == KeeperPresenter.BIND) {
            ARouter.getInstance().build(ARouterUrlKt.KeeperIndexUrl)
                    .navigation(this, new NavigationCallback() {
                        @Override
                        public void onFound(Postcard postcard) {

                        }

                        @Override
                        public void onLost(Postcard postcard) {

                        }

                        @Override
                        public void onArrival(Postcard postcard) {
                            finish();
                            EventBus.getDefault().post(new KeeperEvent(KeeperEvent.BIND));
                        }

                        @Override
                        public void onInterrupt(Postcard postcard) {

                        }
                    });
        }
    }

    @Override
    public void showKeeperList(List<KeeperResult> results) {
        emptyView.hide();
        refreshLayout.finishRefresh(true);
        mPickerAdapter.setList(results);
        if (mPickerAdapter.getItemCount() == 0) {
            emptyView.show("暂没有管家", "");
            tvKeeperDesc.setText("当前小区暂没有管家");
        } else {
            tvKeeperDesc.setText("当前小区有以下管家");
        }
    }

    @Override
    public void showKeeperOderList(List<KeeperOrderResult> results) {

    }
}
